package nero.yncc.com.createart.activity;

import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterSiteMessage;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanMessage;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.ToastUtil;

/**
 * Created by nero on 2017/7/12.
 */

@ContentView(R.layout.single_pulltorefresh)
public class ActivitySiteMessage extends BaseActivity {
    @ViewInject(R.id.pl_list)
    PullToRefreshListView pl_list;
    @ViewInject(R.id.ll_root)
    LinearLayout ll_root;

    int pageSize=10,page=1;
    boolean canLoad=true;
    ArrayList<BeanMessage> arrayList;
    AdapterSiteMessage adapterSiteMessage;
    @Override
    protected void initData() {
        super.initData();
        setTitleBarTitle("站内消息");
        arrayList=new ArrayList<>();
        adapterSiteMessage=new AdapterSiteMessage(mContext,arrayList);
        pl_list.setAdapter(adapterSiteMessage);
        pl_list.setMode(PullToRefreshBase.Mode.BOTH);
        pl_list.setBackgroundColor(getResources().getColor(R.color.lineColorGray2));
    }


    @Override
    protected void initListener() {
        super.initListener();
        pl_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=1;
                canLoad=true;
                arrayList.clear();
                adapterSiteMessage.notifyDataSetChanged();
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });
        pl_list.setRefreshing();
    }

    @Override
    protected void getData() {
        super.getData();
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("pagesize",pageSize+"");
        hashMap.put("pageindex",page+"");

        new HttpUtils().post(this, Constants.API_SITE_MESSAGE, hashMap, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray=(JSONArray)json;
                if(!canLoad){
                    pl_list.onRefreshComplete();
                    showToast("没有更多内容啦");
                    return;
                }
                if(jsonArray.length()<pageSize){
                    canLoad=false;
                }
                for (int a=0;a<jsonArray.length();a++){
                    try {
                        arrayList.add(JSON.parseObject(jsonArray.getString(a),BeanMessage.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                adapterSiteMessage.notifyDataSetChanged();
                pl_list.onRefreshComplete();
            }

            @Override
            public void fail(String failMsg) {
                pl_list.onRefreshComplete();
            }
        });

    }
}
